package com.dq.utils;

import edu.princeton.cs.algs4.In;

import java.lang.reflect.Array;
import java.util.Arrays;

public class CreateArray {
    public static final int INT = 0;
    public static final int STR = 1;
    public static final int CHAR = 2;
    public static Object genIntArray(String s){
        return genArray(s,INT);
    }
    public static Object genCharArray(String s){
        return genArray(s,CHAR);
    }
    public static Object genStrArray(String s){
        return genArray(s,STR);
    }
    //根据此格式生成 多维数组  [[1,2],[1,2],[1,2]]
    //type 0 int, 1 string
    private static Object genArray(String s, int type){
        //获取维度，如 3*4*5 为3维
        int d = getDim(s);
        //获取每个维度的长度 如3*4*5 的长度为[3,4,5]
        int[] dim = getDimArray(s,d);
        //创建数组
        Object[] object = (Object[])Array.newInstance(getCls(type),dim);
        //填充每一个数组
        fill(object,d,s.substring(1,s.length()-1),type);
        return object;
    }
    private static void fill(Object o, int d, String s, int type){
        //如果维度为1， 即到了填充具体的数的部分
        if(d==1){
            String[] temp = s.split(",");
            for (int i = 0; i < Array.getLength(o); i++) {
                switch (type){
                    case INT:
                        Array.setInt(o,i,Integer.parseInt(temp[i]));
                        break;
                    case STR:
                        Array.set(o,i,temp[i]);
                    case CHAR:
                        Array.setChar(o, i, temp[i].trim().charAt(0));
                }
            }
        }else{
            // d>1，代表每一个对象中存储的数组对象
            String[] temp = s.split(getRegex(d));
            Object[] o2 = (Object[])o;
            for (int i = 0; i < o2.length; i++) {
                fill(o2[i], d-1, temp[i].substring(1,temp[i].length()-1),type);
            }
        }
    }
    private static Class getCls(int type){
        switch (type){
            case INT:
                return Integer.TYPE;
            case STR:
                return String.class;
            case CHAR:
                return Character.TYPE;
        }
        return Object.class;
    }
    private static int[] getDimArray(String s, int d){
        // "[[[3,1,2],[3,2,3]],[[1,1,3],[1,2,4]],[[1,1,2],[2,3,4]]]"
        int[] dim = new int[d];
        s = s.substring(1, s.length()-1);
        for (int i = 0; i < dim.length-1; i++) {
            String[] temp = s.split(getRegex(d--));
//            System.out.println(Arrays.toString(temp));
            dim[i] = temp.length;
            s = temp[0].substring(1,temp[0].length()-1);
        }
        dim[dim.length-1] = s.split(",").length;
        return dim;
    }
    private static Object newOneDimArray(String s, int type){
        Class cls= type==CreateArray.INT? Integer.TYPE:String.class;
        String[] strings = s.split(",");
        Object object = Array.newInstance(cls,new int[]{strings.length});
        for (int i = 0; i < strings.length; i++) {
            if(type==CreateArray.INT){
                Array.setInt(object, i, Integer.parseInt(strings[i]));
            }else if(type==CreateArray.STR){
                Array.set(object, i, strings[i]);
            }
        }
        return object;
    }
    private static int getDim(String s){
        int d=0;
        while (s.charAt(d)== '[') d++;
        return d;
    }
    private static String getRegex(int d){
        String left="", right = "";
        for(int i=1;i<d;i++){
            left = left + "]";
            right = right + "\\[";
        }
        left = "(?<="+left+")";
        right = "(?="+right+")";
        String medium = ",";
        return left+medium+right;
    }
    public static void main(String[] args) {

        String s = "[[[1,2],[1,2]],[[1,2],[1,2]],[[1,2],[1,2]],[[1,2],[1,2]]]";
//        String s = "[[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]";

        int[][][] res = (int[][][])CreateArray.genArray(s,0);

        for (int i = 0; i < res.length; i++) {
            System.out.println("##################");
            for (int j = 0; j < res[i].length; j++) {
                System.out.println(Arrays.toString(res[i][j]));
            }
        }

    }
}
